/*******************************************************************************
*  file    : dataobject.cpp
*  created : 07.08.2014
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#include "dataobject.hpp"
#include <interfaces.hpp>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>

DataObject::DataObject(QObject *parent) :
    QObject(parent)
{
    time_ = QDateTime::currentDateTime();
    dataL_ = QSharedPointer<NamedData>(new NamedData);
    dataR_ = QSharedPointer<NamedData>(new NamedData);
}

double DataObject::sampleRate() const
{
    return dataL_->sampleRate;
}

void DataObject::setSampleRate(double sampleRate)
{
    dataL_->sampleRate = sampleRate;
    dataR_->sampleRate = sampleRate;
}

QSharedPointer<NamedData> DataObject::dataL()
{
    QSharedPointer<NamedData> res(dataL_);
    return res;
}

void DataObject::setDataL(QSharedPointer<NamedData> data)
{
    dataL_ = data;
}

void DataObject::setDataL(const std::vector<std::complex<double>> &data)
{
    dataL_->data = data;
}

QSharedPointer<NamedData> DataObject::dataR()
{
    QSharedPointer<NamedData> res(dataR_);
    return res;
}

QWeakPointer<NamedData> DataObject::dataL() const
{
    return QWeakPointer<NamedData>(dataL_);
}

QWeakPointer<NamedData> DataObject::dataR() const
{
    return QWeakPointer<NamedData>(dataR_);
}

void DataObject::setDataR(QSharedPointer<NamedData> data)
{
    dataR_ = data;
}
void DataObject::setDataR(const std::vector<std::complex<double>> &data)
{
    dataR_->data = data;
}

QString DataObject::description() const
{
    return description_;
}

void DataObject::setDescription(const QString &description)
{
    description_ = description;
}
QDateTime DataObject::time() const
{
    return time_;
}

void DataObject::setTime(const QDateTime &time)
{
    time_ = time;
}
double DataObject::temperature() const
{
    return temperature_;
}

void DataObject::setTemperature(double temperature)
{
    temperature_ = temperature;
}
double DataObject::pressure() const
{
    return pressure_;
}

void DataObject::setPressure(double pressure)
{
    pressure_ = pressure;
}

QJsonDocument DataObject::toJsonDocument() const
{
    QJsonDocument document;
    QJsonObject obj;
    obj.insert("name"       , QJsonValue(this->objectName()));
    obj.insert("sample_rate", this->sampleRate());
    obj.insert("description",this->description());
    obj.insert("time"       ,this->time().toString("yyyy.MM.dd HH:mm:ss.zzz"));
    obj.insert("temperature",this->temperature());
    obj.insert("pressure"   , this->pressure());

    QJsonArray arrayL;

    for(auto v : dataL_->data)
        arrayL.push_back(v.real());
    obj.insert("dataL",arrayL);
    obj.insert("dataL_size",static_cast<int>(dataL_->data.size()));

    QJsonArray arrayR;

    for(auto v : dataR_->data)
        arrayR.push_back(v.real());
    obj.insert("dataR",arrayR);
    obj.insert("dataR_size",static_cast<int>(dataR_->data.size()));

    document.setObject(obj);

    return document;
}

void DataObject::updateFromJson(const QJsonDocument &document)
{
    this->setObjectName (document.object()["name"       ].toString());
    this->setDescription(document.object()["description"].toString());
    this->setSampleRate (document.object()["sample_rate"].toInt());
    this->setTemperature(document.object()["temperature"].toDouble());
    this->setPressure   (document.object()["pressure"   ].toDouble());

    QDateTime dt = QDateTime::fromString(document.object()["time"].toString(),"yyyy.MM.dd HH:mm:ss.zzz");
    if( !dt.isValid() )
        dt = QDateTime::fromString(document.object()["time"].toString());

    this->setTime       (dt);

    QJsonArray dataL = document.object()["dataL"     ].toArray();
    int dataL_size   = document.object()["dataL_size"].toInt();
    std::vector<std::complex<double>> dL;
    dL.reserve(dataL_size);
    for(const auto& v : dataL)
        dL.push_back(v.toDouble());

    dataL_->data = dL;

    QJsonArray dataR = document.object()["dataR"     ].toArray();
    int dataR_size   = document.object()["dataR_size"].toInt();
    std::vector<std::complex<double>> dR;
    dR.reserve(dataR_size);
    for(const auto& v : dataR)
        dR.push_back(v.toDouble());

    if(dL.size() != dR.size())
        {
            auto max_sz = std::max(dL.size(),dR.size());
            dL.resize(max_sz);
            dR.resize(max_sz);
        }

    dataL_->data = dL;
    dataR_->data = dR;
}





